package com.wzg.rpc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzg.rpc.mapper.TbItemMapper;
import com.wzg.rpc.pojo.TbItem;
import com.wzg.rpc.pojo.TbItemExample;
import com.wzg.rpc.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.EgoResult;
import pojo.PageResult;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    //注入mapper接口代理对象
    @Autowired
    private TbItemMapper tbItemMapper;
    @Override
    public PageResult<TbItem> selectItemList(Integer page, Integer rows) {
        // TODO Auto-generated method stub

        //执行分页操作
        Page ps = PageHelper.startPage(page, rows);

        TbItemExample example=new  TbItemExample();

        //执行数据库查询操作
        List<TbItem> list = tbItemMapper.selectByExample(example);

        PageResult<TbItem> result = new PageResult<TbItem>();

        result.setRows(list);
        result.setTotal(ps.getTotal());
        return result;
    }
    @Override
    public EgoResult updateItemStatus(List<BigDecimal> itemIds, Boolean flag) {
        // TODO Auto-generated method stub

        //创建TbItem对象
        TbItem item=new TbItem();
        if(flag){
            item.setStatus((short) 1);
        }else{
            item.setStatus((short) 2);
        }

        //动态产生where条件
        TbItemExample example=new TbItemExample();
        TbItemExample.Criteria c = example.createCriteria();
        c.andIdIn(itemIds);

        //where id in(3,4,5)
        //tbItemMapper.updateByExample(item, example);
        tbItemMapper.updateByExampleSelective(item, example);
        //where id=?
        //tbItemMapper.updateByPrimaryKey(record)
        return EgoResult.ok();
    }
    @Override
    public EgoResult deleteItem(List<BigDecimal> itemIds) {
        // where id in(3,4,5)
        //动态产生where条件
        TbItemExample example= new TbItemExample();

        TbItemExample.Criteria c = example.createCriteria();

        c.andIdIn(itemIds);
        tbItemMapper.deleteByExample(example);
        return EgoResult.ok();
    }
}
